home *** CD-ROM | disk | FTP | other *** search
- Path: news2.cais.com!news
- From: Uncle Snuggles <jphelps@nmaa.org>
- Newsgroups: comp.lang.c++
- Subject: Multiple Inheritance Pointer Problem
- Date: Wed, 03 Apr 1996 02:35:02 -0500
- Organization: NMAA
- Message-ID: <31622A26.3633@nmaa.org>
- NNTP-Posting-Host: 198.6.197.171
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (WinNT; I)
-
- I recently used multiple inheritence to help me create a few
- socket classes. The details of these socket classes are not
- important; however, in my pursual of bugs I found that some
- of the pointers to these classes caused core dumps while
- other classes seem to work fine. So, as part of my debug
- process, I printed out pointers to theses classes before I
- passed them around, and then printed these pointers after I
- passed them in to various functions, and to my surprise some
- of these pointers were off by four bytes! (This took place on
- an HP 735, running HP-UX 9.03 -- I think these environment
- specs are correct; its late and I'm groggy.)
-
- At first I thought this was a compiler bug, so I decided to
- encapsulate my class inheritance hierarchy in some code that I
- could try on other platforms besides the HP. The code that
- follows is what I came up with. I tried this code on a P60 with
- MS's 4.0 compiler and got the same pointer problem. Then I
- tried the MS 1.52C compiler and got the same problem except
- that my pointers were off by 2 bytes (the size of a 16 bit
- pointer). I used g++ on the HP and reproduced the same results
- as before. I went to my ISP and used g++ on their machine (x86
- of some sort) which runs BSDI 2.0 and got the, by then, expected
- pointer problem.
-
- I'm tempted to think that there really is no problem. My
- ignorance about multiple inheritence is most likely the culprit.
- Although, a friend of mine looked at Bjorn's C++ book and cited
- a spec that sounds like the only thing that depends on the order
- of multiple inheritence is the calling order of constructors and
- destructors.
-
- So, am I crazy to think that three different compilers on two
- different platforms all have the same incorrect behavior?
- Probably, but I don't have any clue as to why the following
- foobar and barfoo classes are referenced differently when
- printed
- in their parent foo class. If there's anybody out there who'll
- look at the following code and corresponding output (from MFC
- 4.0
- on a Pentium 60) and knows what's going on then please clue me
- in.
-
- One more thing, the following code actually has a pointer
- discrepency of one unless it is compiled with OFF_BY_4 defined
- (this define should actually be OFF_BY_2 on 16 bit systems). I
- can
- almost make sense of the 4 byte discrepency by thinking that not
- all "this"es are created equal, but why a one byte offset?
-
-
-
- #include <iostream.h>
-
- class foo
- {
- public:
- void PrintMe(void) { cerr << "this = " << this << endl; }
- };
-
- class bar
- {
- #ifdef OFF_BY_4
- public:
- int b;
- #endif
- };
-
- class foobar : public foo, public bar
- {
- #ifdef OFF_BY_4
- public:
- int fb;
- #endif
- };
-
- class barfoo : public bar, public foo
- {
- #ifdef OFF_BY_4
- public:
- int bf;
- #endif
- };
-
- main()
- {
- foobar fb;
- barfoo bf;
-
- cerr << "&fb = " << &fb << endl;
- fb.PrintMe();
- cerr << "&bf = " << &bf << endl;
- bf.PrintMe();
-
- return(0);
- }
-
- F:\>cl crap.cpp
- Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
- 10.00.5270 for 80x86
- Copyright (C) Microsoft Corp 1984-1995. All rights reserved.
-
- crap.cpp
- Microsoft (R) 32-Bit Incremental Linker Version 3.00.5270
- Copyright (C) Microsoft Corp 1992-1995. All rights reserved.
-
- /out:crap.exe
- crap.obj
-
- F:\>crap
- &fb = 0x0012FFAC
- this = 0x0012FFAC
- &bf = 0x0012FFA8
- this = 0x0012FFA9
-
- F:\>cl -DOFF_BY_4 crap.cpp
- Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
- 10.00.5270 for 80x86
- Copyright (C) Microsoft Corp 1984-1995. All rights reserved.
-
- crap.cpp
- Microsoft (R) 32-Bit Incremental Linker Version 3.00.5270
- Copyright (C) Microsoft Corp 1992-1995. All rights reserved.
-
- /out:crap.exe
- crap.obj
-
- F:\>crap
- &fb = 0x0012FFA8
- this = 0x0012FFA8
- &bf = 0x0012FFA0
- this = 0x0012FFA4
-